package com.houtai.controller.back;

import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.bean.dto.black.TdBlackDetailDto;
import com.houtai.bean.dto.black.TdBlackListDto;
import com.houtai.bean.vo.black.TdBlackDetailVo;
import com.houtai.bean.vo.black.TdBlackListVo;
import com.houtai.service.black.BlackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value="黑名单接口", tags={"黑名单接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class BackContorller {
    @Autowired
    private BlackService blackService;

    @ApiOperation(value="黑名单列表", notes="黑名单列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/black/list")
    public TdBlackListVo list(@ApiParam(value = "黑名单列表所需参数") @Valid @RequestBody TdBlackListDto dto, BindingResult result) {
        TdBlackListVo retInfo = new TdBlackListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = blackService.blackList(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="黑名单详情", notes="黑名单详情")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/black/detail")
    public TdBlackDetailVo detail(@ApiParam(value = "黑名单详情所需参数") @Valid @RequestBody TdBlackDetailDto dto, BindingResult result) {
        TdBlackDetailVo retInfo = new TdBlackDetailVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = blackService.blackDetail(dto);
        }
        return retInfo;
    }
}
